//
//  NSString+utils.m
//  helper
//
//  Created by huangyi on 12-10-29.
//  Copyright (c) 2012年 huangyi. All rights reserved.
//

#import "NSString+utils.h"
#import "NSData+utils.h"
#import "CharToPinyin.h"
#import "GRMustache.h"

@implementation NSString (utils)

-(NSData*)dataValue{
    return [self dataUsingEncoding:NSUTF8StringEncoding];
}

-(NSUInteger)numberOfLines{
    NSArray *lines=[self componentsSeparatedByString:@"\n"];
    return lines.count;
}

-(NSString*)stringValue{
    return [NSString stringWithString:self];
}

-(BOOL)isValidJSON{
    return self.dataValue.isValidJSON;
}
-(id)jsonObject{
    return self.dataValue.jsonObject;
}
-(id)mutableJsonObject{
    return self.dataValue.mutableJsonObject;
}

-(NSString*)toPinYin{
    NSString* py=[[CharToPinyin shared] translate:self withSpaceString:@"" options:CPSpellOptionsFirstLetterOnly];
    return py;
}

-(NSString*)toPinYinWithSeparator:(NSString*)sep options:(int)options{
    NSString* py=[[CharToPinyin shared] translate:self withSpaceString:sep options:options];
    return py;
}

-(NSDictionary*)toDictionary{
    if (self.length) {
        NSMutableDictionary *d=[NSMutableDictionary dictionary];
        NSArray *params=[self componentsSeparatedByString:@"&"];
        for (NSString *row in params) {
            NSArray *kv=[row componentsSeparatedByString:@"="];
            if (kv.count>0) {
                NSString *k=[kv objectAtIndex:0];
                if (kv.count>1) {
                    NSString *v=[kv objectAtIndex:1];
                    [d setValue:v forKey:k];
                }
            }
        }
        return d;
    }else{
        return nil;
    }
}

+(NSString*)concatStrings:(NSString*)str,...NS_REQUIRES_NIL_TERMINATION{
    NSArray *result=nil;
    if(str!=nil){
        NSMutableArray *args=[NSMutableArray array];
        va_list vl;
        id argv=str;
        va_start(vl,str);
        while(argv!=nil){
            [args addObject:argv];
            argv=(__bridge id)(va_arg(vl, void*));
        }
        va_end(vl);
        result=[NSArray arrayWithArray:args];
    }
    return [result componentsJoinedByString:@""];
}

-(NSDate*)dateWithFormat:(NSString*)format{
    static NSDateFormatter *dateFormatter=nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dateFormatter=[[NSDateFormatter alloc] init];
    });
    dateFormatter.dateFormat=format;
    NSDate *date=[dateFormatter dateFromString:self];
    return date;
}

-(NSData*)dataFromHex{
    NSMutableData *data= [[NSMutableData alloc] init];
    unsigned char whole_byte;
    char byte_chars[3] = {'\0','\0','\0'};
    int i;
    int length=self.length/2;
    for (i=0; i < length; i++) {
        byte_chars[0] = [self characterAtIndex:i*2];
        byte_chars[1] = [self characterAtIndex:i*2+1];
        whole_byte = strtol(byte_chars, NULL, 16);
        [data appendBytes:&whole_byte length:1];
    }
    return data;
}

@end


NSString* NSStringFromCString(char *str){
    return [NSString stringWithUTF8String:str];
}

NSString* uniqueID(){
    NSString *identifierString;
    CFUUIDRef uuidObject = CFUUIDCreate(NULL);
    CFStringRef uuidString=CFUUIDCreateString(NULL, uuidObject);
    identifierString=[NSString stringWithString:(__bridge NSString*)uuidString];
    CFRelease(uuidString);
    CFRelease(uuidObject);
    return identifierString.lowercaseString;
}

NSString* MustacheTemplate(NSString *mustache,id object){
    if (mustache==nil) {
        NSObject *obj=object;
        return obj.description;
    }
    NSError *error=nil;
    NSString *str=[GRMustacheTemplate renderObject:object fromString:mustache error:&error];
    if (error) {
        NSLog(@"%@",error.description);
    }
    return str;
}
